package hl.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;

import hl.dao.BaseDao;
import hl.model.TStuff;

@Transactional
@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDao<T> {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	
	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}
	
	@Override
	public T get(String hql) {
		Query q= this.getCurrentSession().createQuery(hql);
		List<T> l=q.list();
		if(l!=null&& l.size()>0)
		{
			return l.get(0);
		}
		return null;
	}

	@Override
	public T get(String hql, Object[] params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null && params.length>0) {
			for (int i=0;i<params.length;i++)
				q.setParameter(i, params[i]);
		}
		List<T> l=q.list();
		if(l!=null&& l.size()>0)
		{
			return l.get(0);
		}
		return null;
	}
	
	@Override
	public T get(String hql, Map<String, Object> params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()) {
			for(String key:params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		List<T> l=q.list();
		if(l!=null&& l.size()>0)
		{
			return l.get(0);
		}
		return null;
	}
	
	@Override
	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	@Override
	public void update(T o) {
		this.getCurrentSession().update(o);
	}

	@Override
	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}

	@Override
	public List<T> find(String hql) {
		Query q= this.getCurrentSession().createQuery(hql);
		return q.list();
	}

	@Override
	public List<T> find(String hql, Object[] params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null && params.length>0) {
			for (int i=0;i<params.length;i++)
				q.setParameter(i, params[i]);
		}
		return q.list();
	}



	@Override
	public List<T> find(String hql, Map<String, Object> params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()) {
			for(String key:params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return q.list();
	}

	@Override
	public List<T> find(String hql, Object[] params, int page, int rows) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null && params.length>0) {
			for (int i=0;i<params.length;i++)
				q.setParameter(i, params[i]);
		}
		//firstResult=(当前页-1)*每页记录数
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}

	@Override
	public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()) {
			for(String key:params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}

	@Override
	public List<T> find(String hql, int page, int rows) {
		Query q= this.getCurrentSession().createQuery(hql);
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}

	@Override
	public int count(String hql) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(q.list().size()>0&&q!=null) {
			return  q.list().size();
		}
		return 0;
	}

	@Override
	public int count(String hql, Object[] params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null && params.length>0) {
			for (int i=0;i<params.length;i++)
				q.setParameter(i, params[i]);
		}
		if(q.list().size()>0&&q!=null) {
			return  q.list().size();
		}
		return 0;
		//return  (int) q.uniqueResult();
	}

	@Override
	public int count(String hql, Map<String, Object> params) {
		Query q= this.getCurrentSession().createQuery(hql);
		if(params!=null&&!params.isEmpty()) {
			for(String key:params.keySet()) {
				q.setParameter(key, params.get(key));
			}
		}
		if(q.list().size()>0&&q!=null) {
			return  q.list().size();
		}
		return 0;
		//return  (int) q.uniqueResult();
	}

	@Override
	public T get(Class<T> c, Serializable id) {
		
		return (T) this.getCurrentSession().get(c,id);
	}

	@Override
	public List getList(Class<T> c,String sql) {
		return this.getCurrentSession().createSQLQuery(sql).addEntity(c).list();
	}

}
